package com.tbit.common.utils;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

/**
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @version 4.3.0
 * @since 2025/1/7 10:27
 */
public final class MessageUtil {

    /**
     * 获取国际化翻译值
     */
    public static String get(String msgKey) {
        return getInstance().getMessage(msgKey, null, LocaleContextHolder.getLocale());
    }

    /**
     * 获取中文翻译值
     */
    public static String getWithChina(String msgKey) {
        return getInstance().getMessage(msgKey, null, Locale.CHINA);
    }

    /**
     * 获取国际化翻译值（包括占位符）
     */
    public static String get(String msgKey, Object... objects) {
        return getInstance().getMessage(msgKey, objects, LocaleContextHolder.getLocale());
    }

    private static MessageSource getInstance() {
        return Lazy.MESSAGE_SOURCE;
    }

    /**
     * 使用懒加载方式实例化messageSource国际化工具
     */
    private static class Lazy {
        private static final MessageSource MESSAGE_SOURCE = SpringUtil.getBean(MessageSource.class);
    }

}
